06. Using a CursorLoader

Using a CursorLoader

Question:

Start Quiz:

Solution:

INSTRUCTOR NOTE:

Refer to this tutorial or this one

**Hints: **

  • The keyboard shortcut for adding methods that need to be implemented from an interface: Ctrl + I
  • Make CursorAdapter a global variable
  • For the onLoadFinished() and onLoaderReset() methods, you can use the CursorAdapter’s swapCursor() method to change the data source Cursor.

Notes:

  • Inserting new pets (from dummy menu item or editor) will not update the list of pets automatically (until the next coding task). You need to force stop the app and open it again, to see the list of pets update.

If you’re wondering why the projection needs to include _id, see the description for the CursorAdapter class. The CursorAdapter assumes that the Cursor contains a column called _id.

  • Lastly, if you encounter errors regarding the imported libraries you may need to import the support libraries. So this:
import android.support.v7.app.LoaderManager;
import android.support.v7.content.CursorLoader;
import android.support.v7.content.Loader;
import android.support.v7.widget.SimpleCursorAdapter;

instead of what is shown in the video:

import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.widget.SimpleCursorAdapter;